package cn.iocoder.an.module.business.controller.admin.reportconfig;

import cn.iocoder.an.module.business.dal.dataobject.reportviewnew.ReportConfigDO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.reportconfig.vo.*;
import cn.iocoder.an.module.business.service.reportconfig.ReportConfigService;

@Tag(name = "管理后台 - 报表配置")
@RestController
@RequestMapping("/business/report-config")
@Validated
public class ReportConfigController {

    @Resource
    private ReportConfigService reportConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建报表配置")
    @PreAuthorize("@ss.hasPermission('business:report-config:create')")
    public CommonResult<Long> createReportConfig(@Valid @RequestBody ReportConfigSaveReqVO createReqVO) {
        return success(reportConfigService.createReportConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报表配置")
    @PreAuthorize("@ss.hasPermission('business:report-config:update')")
    public CommonResult<Boolean> updateReportConfig(@Valid @RequestBody ReportConfigSaveReqVO updateReqVO) {
        reportConfigService.updateReportConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报表配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:report-config:delete')")
    public CommonResult<Boolean> deleteReportConfig(@RequestParam("id") Long id) {
        reportConfigService.deleteReportConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报表配置")
                @PreAuthorize("@ss.hasPermission('business:report-config:delete')")
    public CommonResult<Boolean> deleteReportConfigList(@RequestParam("ids") List<Long> ids) {
        reportConfigService.deleteReportConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报表配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:report-config:query')")
    public CommonResult<ReportConfigRespVO> getReportConfig(@RequestParam("id") Long id) {
        ReportConfigDO reportConfig = reportConfigService.getReportConfig(id);
        return success(BeanUtils.toBean(reportConfig, ReportConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报表配置分页")
    @PreAuthorize("@ss.hasPermission('business:report-config:query')")
    public CommonResult<PageResult<ReportConfigRespVO>> getReportConfigPage(@Valid ReportConfigPageReqVO pageReqVO) {
        PageResult<ReportConfigDO> pageResult = reportConfigService.getReportConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报表配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:report-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportConfigExcel(@Valid ReportConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportConfigDO> list = reportConfigService.getReportConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报表配置.xls", "数据", ReportConfigRespVO.class,
                        BeanUtils.toBean(list, ReportConfigRespVO.class));
    }

}